const ProjectRequest = require("../project/project_request.js");
const Url = require("../utils/url.js");

/**
 * 创建购买订单
 * 
 */
function create(data, callback) {
  const url = "/order/product/create";
  // 发送请求
  ProjectRequest.postRequest(
    url, 
    data,
    callback
  );
}

/**
 * 创建包装礼盒订单
 * 
 */
function createPack(products, addressData, callback) {
  const url = "/order/product/pack/create";
  ProjectRequest.postRequest(
    url, 
    { 
      products: products,
      address: addressData
    },
    callback
  );
}

/**
 * 创建易物(积分)订单
 * 
 */
function createGrade(products, callback) {
  const url = "/order/product/grade/create";
  ProjectRequest.postRequest(url, products, callback);
}
function deleteGrade(id, callback) {
  const url = "/barter/order/" + id;
  ProjectRequest.deleteRequest(url, callback);
}
function findGradeById(orderId, callback) {
  const url = "/barter/order/" + orderId;
  ProjectRequest.getRequest(url, callback);
}
function pageGrade(startPage, callback) {
  const URL = Url.newInstance("/barter/order/page");
  URL.setPara('startPage', startPage)
  ProjectRequest.getRequest(URL.get(), callback);
}
function gradeOrderPay(id, data, callback) {
  const url = "/barter/order/" + id + "/pay";
  ProjectRequest.postRequest(url, data, callback);
}

function findById(orderId, callback) {
  const url = "/order/product/" + orderId;
  ProjectRequest.getRequest(url, callback);
}

function findByIdWithProducts(orderId, callback) {
  const URL = Url.newInstance("/order/product/" + orderId);
  URL.setPara('showProduction', true)
  ProjectRequest.getRequest(URL.get(), callback);
}

function list(state, callback) {
  const url = "/order/product/page?state=" + state;
  ProjectRequest.getRequest(url, callback);
}

function pagePack(startPage, callback) {
  const URL = Url.newInstance("/pack/order/page");
  URL.setPara('startPage', startPage)
  ProjectRequest.getRequest(URL.get(), callback);
}

function gainOrderStatistics(callback){
  const URL = Url.newInstance("/order/product/statistics");
  ProjectRequest.getRequest(URL.get(), callback);
}

/**
 * exports
 */
module.exports = {
  create, 
  createGrade, deleteGrade, 
  createPack,
  findById, findByIdWithProducts,
  findGradeById,
  list, 
  pageGrade, pagePack,
  gradeOrderPay,
  gainOrderStatistics   // 获取订单统计数据
};